<template>
<div class="">
  <!-- 面包屑导航区域 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品分类</el-breadcrumb-item>
  </el-breadcrumb>
  <!-- 卡片视图区域 -->
  <el-card>
    <el-row>
      <el-col>
        <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      </el-col>
    </el-row>
    <!-- 分类表格区域 -->
    <el-table :data="cateList" row-key="cat_id" border :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
      <el-table-column prop="cat_deleted" label="是否有效">
        <template v-slot="scope">
          <i v-if="scope.row.cat_deleted === false" class="el-icon-success" style="color: lightgreen"></i>
          <i v-else class="el-icon-success" style="color: red"></i>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="排序">
        <template v-slot="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <el-button type="primary">编辑</el-button>
        <el-button type="danger">删除</el-button>
      </el-table-column>
    </el-table>
    <el-pagination class="page" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
  </el-card>
  <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
    <el-form :model="addCateForm" :rules="addCateFormRules" label-width="100px" ref="addCateFormRef">
      <el-form-item label="分类名称" prop="cat_name">
        <el-input v-model="addCateForm.cat_name" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="父级分类">
        <el-cascader v-model="selectedParentCateId" :options="parentCateList" :props="parentCateProps" @change="parentCateChange" change-on-select></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="addCateDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addCate">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 商品分类列表
      cateList: [],
      // 商品类数目
      total: 0,
      // 控制添加分类对话框的显示
      addCateDialogVisible: false,
      // 添加分类的表单数据对象
      addCateForm: {
        // 新添加的分类的父级id，默认是一级
        cat_pid: 0,
        // 新添加的分类的名称
        cat_name: "",
        // 新添加的分类的等级，默认是一级
        cat_level: 0,
      },
      addCateFormRules: {
        cat_name: [{
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      // 从服务器获取到的父级分类
      parentCateList: [],
      // 级联选择器选中的父级分类的id
      selectedParentCateId: [],
      // 父级分类的数据的展示配置项
      parentCateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true,
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 获取商品分类列表
    async getCateList() {
      const {
        data: res
      } = await this.$request("categories", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("服务器错误，获取商品分类失败");
      }
      this.cateList = res.data.result;

      this.total = res.data.total;
    },
    // 监听 pagesize 改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    // 监听 pagenum 改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
    // 显示添加分类对话框
    showAddCateDialog() {
      this.getParentCateList();
      this.addCateDialogVisible = true;
    },
    // 获取一级、二级的分类
    async getParentCateList() {
      const {
        data: res
      } = await this.$request("categories", {
        params: {
          type: 2,
        },
      });
      if (res.meta.status !== 200)
        return this.$message.error("服务器错误，获取父级分类失败");
      this.parentCateList = res.data;
    },
    // 监听级联选择器的改变
    parentCateChange() {
      if (this.selectedParentCateId.length > 0) {
        this.addCateForm.cat_pid = this.selectedParentCateId[
          this.selectedParentCateId.length - 1
        ];
        this.addCateForm.cat_level = this.selectedParentCateId.length;
        return;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    // 监听对话框添加分类对话框的关闭，清空新分类名称、新分类的level、pid
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields();
      this.selectedParentCateId = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    },
    // 监听添加分类确认按钮，
    addCate() {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return;
        const {
          data: res
        } = await this.$request.post(
          "categories",
          this.addCateForm
        );
        if (res.meta.status !== 201)
          return this.$message.error("服务器错误，添加分类失败");
        this.$message.success("添加分类成功");
        this.getCateList();
        this.addCateDialogVisible = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>
